package com.hu.zhcc.shiro.controller.xy;

import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.lgx.TMemberExamination;
import com.hu.zhcc.shiro.entity.xy.dto.TAssessDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssess;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.vo.TAssessListVO;
import com.hu.zhcc.shiro.entity.xy.vo.TMemberExaminationListVo;
import com.hu.zhcc.shiro.service.lgx.TMemberExaminationService;
import com.hu.zhcc.shiro.service.lgx.TmemberService;
import com.hu.zhcc.shiro.service.xy.TAssessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;


@RestController
@RequestMapping("/tAssess")
public class TAssessController {

    @Autowired
    private TAssessService service;
    @Autowired
    private TmemberService tmemberService;

    @Autowired
    private TMemberExaminationService tMemberExaminationService;

    //修改答案
    @PutMapping("/updateTAssess/{id}")
    public Integer updateTBodyType(@PathVariable("id") int id, @RequestBody String answers) {
//        System.out.println(id);
        TAssessQuestion[][] objects = JSONObject.parseObject(answers, TAssessQuestion[][].class);
        return service.updateTAssess(id,objects);
    }

    //添加答案
    @PostMapping("/addTAssess/{id}")
    public Integer addTAssess(@PathVariable("id") String idcard, @RequestBody String answers) {
//        System.out.println(id);
        TAssessQuestion[][] objects = JSONObject.parseObject(answers, TAssessQuestion[][].class);
        return service.addTAssess(idcard,objects);
    }

    @GetMapping("")
    public ResponseEntity<Page<TAssessListVO>> getTBodyTypeList(@RequestParam(required = false) String name,
                                                                int offset, int limit) throws ParseException {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }

        Page<TAssessDTO> result = this.service.listTAssess(paremeters, offset, limit);
        List<TAssessListVO> voList = new ArrayList<TAssessListVO>();
        if(result.getRows()==null){
            return ResponseEntity.ok(new Page<TAssessListVO>(result.getTotal(), voList));
        }

        for(TAssessDTO dto : result.getRows()) {
            TAssessListVO vo = new TAssessListVO();
            BeanUtils.copyProperties(dto, vo);
            voList.add(vo);
        }
        return ResponseEntity.ok(new Page<TAssessListVO>(result.getTotal(), voList));
    }

    @GetMapping("/getTAssessById")
   public ResponseEntity<TAssessListVO> getTAssessById(@RequestParam(required = true) Integer memberId){
       TAssess tAssess = service.selectMember(memberId);
       if (tAssess==null){
           return ResponseEntity.notFound().build();
       }
//        System.out.println(tAssess.toString());
       TAssessListVO vo = new TAssessListVO();
       BeanUtils.copyProperties(tAssess,vo);
       return ResponseEntity.ok(vo);
   }


//   模块2查询出体检信息
    @GetMapping("/selectWithSelMeat")
    public ResponseEntity<TMemberExaminationListVo> selectMany(@RequestParam(required = true) Integer memberId){
        System.out.println(memberId);
        TMemberExamination tMemberExaminations = tMemberExaminationService.selectMany(memberId);
        System.out.println(tMemberExaminations.toString());
//        List<TMemberExaminationListVo> listVos = new ArrayList<>();
//        for (TMemberExamination me :
//                tMemberExaminations) {
            TMemberExaminationListVo vo = new TMemberExaminationListVo();
            BeanUtils.copyProperties(tMemberExaminations,vo);
//            listVos.add(vo);
//        }

        return ResponseEntity.ok(vo);
    }

}
